//
//  AppDelegate.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/20.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import IQKeyboardManager
import SwiftyJSON

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    fileprivate let redirectURL = "http://mobile.umeng.com/social"
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        setUMShare()
        setGaodeMap()
        setThirdLibs()
        createWindow()
        
        
            
        //定位当前城市
        KMTool.getCuttentLocation(block: nil)
        
        //获取用户信息
        if KMTool.isSignIn() {
            KMTool.getUserNews(userName: KMTool.stringForEnumKey(.phone) ?? "", success: nil)
        }
        
        
        return true
    }
    
    
    //MARK: 设置友盟分享
    fileprivate func setUMShare() {
        
        //关闭https强制验证
        UMSocialGlobal.shareInstance().isUsingHttpsWhenShareContent = false
        //打开日志
        UMSocialManager.default().openLog(true)
        UMConfigure.initWithAppkey(shareKey, channel: nil)
        
        //微信
        UMSocialManager.default().setPlaform(.wechatSession, appKey: "wx0bc5da978c8bc33a", appSecret: "d188f15ad5ee4938fb11d2adf6895a47", redirectURL: redirectURL)
        
        //微信朋友圈
        UMSocialManager.default().setPlaform(.wechatTimeLine, appKey: "wx0bc5da978c8bc33a", appSecret: "d188f15ad5ee4938fb11d2adf6895a47", redirectURL: redirectURL)
        
        //qq
        UMSocialManager.default().setPlaform(.QQ, appKey: "101501923", appSecret: nil, redirectURL: redirectURL)
        
        //qq空间
        UMSocialManager.default().setPlaform(.qzone, appKey: "101501923", appSecret: nil, redirectURL: redirectURL)
        
        //新浪微博
        UMSocialManager.default().setPlaform(.sina, appKey: "1027667200", appSecret: "a7c558bf5cec9ac248ef2032b9e2f8ae", redirectURL: redirectURL)
    }
    
    //MARK: 设置高德地图
    fileprivate func setGaodeMap() {
        
        AMapServices.shared().apiKey = mapKey
        AMapServices.shared().enableHTTPS = true //开启https验证
        
        
        
    }
    
    //MARK: 设置第三方库
    fileprivate func setThirdLibs() {
        
        //设置键盘
        IQKeyboardManager.shared().isEnabled = true
    }
    
    //MARK: 创建window
    fileprivate func createWindow() {
        
        window = UIWindow.init(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        window?.makeKeyAndVisible()
        window?.rootViewController = RootTabBarController()
    }
    

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    //MARK: 分享成功回调
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        
        let result = UMSocialManager.default().handleOpen(url)
        
        if !result {
            
        }
        
        return result
    }


}

